package com.mathset.business.service.impl;

import java.sql.Date;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mathset.bean.Question;
import com.mathset.business.dao.IQuestionDao;
import com.mathset.business.service.IQuestionService;
import com.shucao.formula.base.unit.PackUnit;
import com.shucao.formula.parser.FormulaFormatImproved;
import com.shucao.formula.parser.FormulaStructureImproved;

@Service
public class QuestionService implements IQuestionService{

	@Autowired
	private IQuestionDao iQuestionDao;
	
	@Override
	public void addQuestion(Question question) {
		question.setCreateTime(new Date(new java.util.Date().getTime()));
		iQuestionDao.submitQuestion(question);
		
	}

	@Override
	public void updateQuestion(Question question) {
		question.setLastUpdate(new Date(new java.util.Date().getTime()));
		iQuestionDao.updateQuestion(question);
	}

	@Override
	public List<Question> retrieveQuestions(int startIndex) {
		List<Question> list = iQuestionDao.retrieveQuestions(startIndex);
		
		Iterator<Question> iterator = list.iterator();
		while(iterator.hasNext()) {
			Question q = iterator.next();
			String content = "\\分数{1}{x}";//
			
			if (content != null && !content.equals("")) {
				FormulaStructureImproved fsi = new FormulaStructureImproved();
				fsi.setContent(content);
				fsi.parse();
				FormulaFormatImproved ffi = new FormulaFormatImproved(fsi);
				int size = 20;
				try {
					
					content = PackUnit.packHtml(ffi.parse(), size);
					q.setContent(content);
				} catch (InstantiationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		}
		
		return list;
	}

	@Override
	public Question retrieveQuestionById(int id) {
		return iQuestionDao.selectQuestionById(id);
	}


}
